package com.kaiyidiannao.common.util;

import android.util.Log;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by wzg on 14-4-10.
 */
public class SearchData {

    private HttpGet request = new HttpGet();
    private final DefaultHttpClient client = new DefaultHttpClient();
    private String resultStr;


    public String SearchUrl (String url) {
        Log.d("http:sina ", url);
        request = new HttpGet(url);
        int timeout = 30000;
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setSoTimeout(params, timeout);
        HttpConnectionParams.setConnectionTimeout(params, timeout);
        requestHttp(request);
        client.setParams(params);
        return resultStr;
    }

    public void requestHttp(HttpGet request) {
        try {
            HttpResponse response = client.execute(request);
            InputStream inputStream = response.getEntity().getContent();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int length;
            while ((length = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, length);
            }
            outputStream.close();
            inputStream.close();
            resultStr = outputStream.toString("UTF-8");
            Log.d("http:result + ", resultStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
